﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using BL;
using Entities;

namespace WindowsFormsApplication1
{
    public partial class frmTheoDoiDonHang : Form
    {
        DonHangBL donHangBL;
        TheoDoiCongNhanBL theoDoiCongNhanBL;
        BindingSource bstheodoidonhangchung = new BindingSource();
        DataTable dttheodoidonhangchung = null;
        DataSet dstheodoidonhangchung;

        DataSet dstheoDoiDonHangChiTiet = null;

        ThongTinDonHangBL thongTinDonHangBL;

        CongNhanBL congNhanBL;
        DataTable dtchoncongnhan = null;

        SanPhamBL sanPhamBL;
        BindingSource bssp = new BindingSource();
        DataTable dtSanPham = null;
        DataSet dsSanPham;

        ComboBox currentSelectedCmbMaSanPham = null;

        DA.BaseConnection.DataPacket dpDsTheoiDoiCongNhan;

        public frmTheoDoiDonHang()
        {
            InitializeComponent();

            donHangBL = new DonHangBL();
            thongTinDonHangBL = new ThongTinDonHangBL();
            congNhanBL = new CongNhanBL();
            sanPhamBL = new SanPhamBL();
            theoDoiCongNhanBL = new TheoDoiCongNhanBL();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            try 
            {
            dgvTheoDoiDonHangChung.AutoGenerateColumns = false;
            dstheodoidonhangchung = donHangBL.theoDoiDonHangChung();
            bstheodoidonhangchung.DataSource = dstheodoidonhangchung.Tables[0];
            dgvTheoDoiDonHangChung.DataSource = bstheodoidonhangchung;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmbChonDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cmbMaSanPham.DataSource = null;
                cmbMaSanPham.Items.Clear();                
                if (cmbChonDonHang.SelectedItem != null && !cmbChonDonHang.SelectedItem.ToString().Equals(""))
                {
                    DataTable dtSanPhamTheoDonHang = donHangBL.laySanPhamTheoMaDonHang(cmbChonDonHang.SelectedItem.ToString()).Tables[0];
                    //foreach (DataRow item in dtSanPhamTheoDonHang.Rows)
                    //{
                    //    cmbMaSanPham.Items.Add(item[0].ToString());
                    //}
                    //cmbMaSanPham.Selected.ToString();
                    //////////////
                    cmbMaSanPham.DataSource = dtSanPhamTheoDonHang;
                    cmbMaSanPham.DisplayMember = "MaSanPham";
                    cmbMaSanPham.ValueMember = "MaSanPham";
                   
                }
                //for (int i = 0; i < dgvNhapBTP.Rows.Count; i++)
                //{
                //    dgvNhapBTP.Rows[i].Cells["MaDongHangInvisible"].Value = cmbChonDonHang.SelectedItem.ToString();
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //    cmbMaSanPham.Items.Clear();
                                     
        }

        private void cmbMaSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            //dgvNhapBTP.AutoGenerateColumns = false;
            //dsSanPham = sanPhamBL.thongTinSanPham(cmbMaSanPham.Selected.ToString());
            //bssp.DataSource = dsSanPham.Tables[0];
            //dgvNhapBTP.DataSource = bssp;
            try
            {
                if (currentSelectedCmbMaSanPham != null 
                    && currentSelectedCmbMaSanPham.SelectedValue != null 
                    && !currentSelectedCmbMaSanPham.SelectedValue.ToString().Equals("")                    
                   )
                {
                    dtSanPham = sanPhamBL.thongTinSanPham(currentSelectedCmbMaSanPham.SelectedValue.ToString()).Tables[0];
                    dgvNhapBTP.CurrentRow.Cells["txtSoMau"].Value = dtSanPham.Rows[0][1].ToString();
                    dgvNhapBTP.CurrentRow.Cells["txtKichThuoc"].Value = dtSanPham.Rows[0][0].ToString();
                    currentSelectedCmbMaSanPham = null;            
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //cmbMaSanPham.Items.Clear();

        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                dgvTheoDoiDonHangChiTiet.AutoGenerateColumns = false;
                dstheoDoiDonHangChiTiet = donHangBL.theoDoiDonHangChiTiet(txtMaDonHang.Text);
                dgvTheoDoiDonHangChiTiet.DataSource = dstheoDoiDonHangChiTiet.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }            
        }

        private void frmTheoDoiDonHang_Load(object sender, EventArgs e)
        {
           try
            {
                dttheodoidonhangchung = thongTinDonHangBL.chonDonHang().Tables[0];
                foreach (DataRow item in dttheodoidonhangchung.Rows)
                {
                    cmbChonDonHang.Items.Add(item[0].ToString());
                }
                //cmbChonDonHang.DataSource = dttheodoidonhangchung;
                //cmbChonDonHang.DisplayMember = "MaDonHang";
                //cmbChonDonHang.ValueMember = "MaDonHang";

               // cmbChonDonHang.SelectedIndex = 0;

                /////////////////
                dtchoncongnhan = congNhanBL.chonCongNhan().Tables[0];
                //foreach (DataRow item in dtchoncongnhan.Rows) 
                //{
                //    cmbMaCongNhan.Items.Add(item[0].ToString());
                //}
                //cmbMaCongNhan.Selected.ToString();
                cmbMaCongNhan.DataSource = dtchoncongnhan;
                cmbMaCongNhan.DisplayMember = "TenCongNhan";
                cmbMaCongNhan.ValueMember = "MaCongNhan";

                dgvNhapBTP.AutoGenerateColumns = false;
                dpDsTheoiDoiCongNhan = theoDoiCongNhanBL.layDanhSachTheoDoiCongNhanDungDeInsert();
                //bssp.DataSource = dpDsTheoiDoiCongNhan.ds.Tables[0];
                //dgvNhapBTP.DataSource = bssp;
                                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }          
        }

        private void dgvNhapBTP_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
           try
           {
                if (dgvNhapBTP.CurrentCell.OwningColumn.Name.Equals("cmbMaSanPham"))
                {
                    currentSelectedCmbMaSanPham = e.Control as ComboBox;
                    ComboBox tmpCmbMaSanPham = e.Control as ComboBox;
                    tmpCmbMaSanPham.SelectedIndexChanged += cmbMaSanPham_SelectedIndexChanged;
                }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
            //cmbMaSanPham.Items.Clear();

            
            //if (e.Control.Equals(cmbMaSanPham))
            //{
            //    DataTable dtSanPham = sanPhamBL.thongTinSanPham(cmbMaSanPham.Selected.ToString()).Tables[0];
            //    foreach (DataRow item in dtSanPham.Rows)
            //    {
            //        //txtKichThuoc
            //        txtSoMau.Selected.ToString();
            //    }
            //    //txtKichThuoc.Selected.ToString();
            //    //txtSoMau.Selected.ToString();

            //}
        }

        private void dgvNhapBTP_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
            //if (e.ColumnIndex == 1)
            //{
            //    DataTable dtSanPham = sanPhamBL.thongTinSanPham(cmbMaSanPham.Selected.ToString()).Tables[0];
            //    dgvNhapBTP.Rows[e.RowIndex].Cells[2].Value = dtSanPham.Rows[0][0].ToString();
            //    dgvNhapBTP.Rows[e.RowIndex].Cells[3].Value = dtSanPham.Rows[0][1].ToString();
                
            //}
        }

        private void dgvNhapBTP_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongChatLuong() == true)
                {
                    if (dgvNhapBTP.CurrentRow.Cells["SanLuong"].Value != null && !dgvNhapBTP.CurrentRow.Cells["SanLuong"].Value.ToString().Equals("")
                        && dgvNhapBTP.CurrentRow.Cells["PhePham"].Value != null && !dgvNhapBTP.CurrentRow.Cells["PhePham"].Value.ToString().Equals("")
                        && dgvNhapBTP.CurrentRow.Cells["cmbMaSanPham"].Value != null && dgvNhapBTP.CurrentRow.Cells["cmbMaCongNhan"].Value != null
                       )
                    {
                        // cmbMaSanPham.Items.Clear();
                        String maSanPham = dgvNhapBTP.CurrentRow.Cells["cmbMaSanPham"].Value.ToString();
                        String maCongNhan = dgvNhapBTP.CurrentRow.Cells["cmbMaCongNhan"].Value.ToString();
                        String maDonHang = cmbChonDonHang.SelectedItem.ToString();

                        String ngayTheoDoi = dtpNgayTheoDoi.Value.ToString("yyyy/MM/dd");

                        try
                        {
                            String sanLuong = dgvNhapBTP.CurrentRow.Cells["SanLuong"].Value.ToString();
                            String phePham = dgvNhapBTP.CurrentRow.Cells["PhePham"].Value.ToString();
                            if (!maDonHang.Equals("") && !maCongNhan.Equals(""))
                            {
                                theoDoiCongNhanBL.capNhatDanhSachTheoDoiCongNhan(maDonHang, maCongNhan, maSanPham, sanLuong, phePham, ngayTheoDoi);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }                
            //}
            //catch (Exception ex)
            //{
              //  MessageBox.Show(ex.Message);
            //}
        }

        private void dgvNhapBTP_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongChatLuong() == true)
                {
                    this.Invoke((MethodInvoker)delegate
                    {
                        dttheodoidonhangchung = thongTinDonHangBL.chonDonHang().Tables[0];
                        foreach (DataRow item in dttheodoidonhangchung.Rows)
                        {
                            cmbChonDonHang.Items.Add(item[0].ToString());
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }                       
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                frmKiemTraKTBTP frm = new frmKiemTraKTBTP();
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dgvTheoDoiDonHangChiTiet_RowValidated(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnXemPhieuNhapBTP_Click(object sender, EventArgs e)
        {
            try
            {
                if (cmbChonDonHang.SelectedItem != null && !cmbChonDonHang.SelectedItem.ToString().Equals(""))
                {
                    DataSet ds = theoDoiCongNhanBL.layDanhSachTheoDoiCongNhan(dtpNgayTheoDoi.Value.ToString("yyyy/MM/dd"), cmbChonDonHang.SelectedItem.ToString());
                    dgvNhapBTP.DataSource = ds.Tables[0];

                    //DataTable dtSanPham = sanPhamBL.thongTinSanPham(currentSelectedCmbMaSanPham.SelectedValue.ToString()).Tables[0];

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataGridViewComboBoxCell test = ((DataGridViewComboBoxCell)dgvNhapBTP.Rows[i].Cells["cmbMaSanPham"]);
                        test.Value = ds.Tables[0].Rows[i]["MaSanPham"];

                        DataTable dtSanPham = sanPhamBL.thongTinSanPham(ds.Tables[0].Rows[i]["MaSanPham"].ToString()).Tables[0];

                        dgvNhapBTP.Rows[i].Cells["txtSoMau"].Value = dtSanPham.Rows[0][1].ToString();
                        dgvNhapBTP.Rows[i].Cells["txtKichThuoc"].Value = dtSanPham.Rows[0][0].ToString();
                    }                  
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            //foreach (DataRow item in ds.Tables[0].Rows)
            //{
            //    DataGridViewRow dgvr = new DataGridViewRow();
            //    dgvNhapBTP.Rows.Add(dgvr);
            //    ((DataGridViewComboBoxCell)dgvr.Cells["cmbMaSanPham"]).Value = item["MaSanPham"].ToString();
            //}
        }
    }
}
